<template>
	<view class="page">
		<view class="con">
		<!-- 	<view class="">
				<button type="default" @click="upvido()">上传视频</button>
			</view> -->
			<view class="uni-list" v-if="listData.length != 0">
				
					<view   >
						
						<uni-swipe-action>
							<uni-swipe-action-item  hover-class="uni-list-cell-hover" :right-options="options"  @click="bindClick($event,index)" @change="swipeChange($event, index)"  v-for="(item, index) in listData" :key="index">
								
								<view class="content-box"  @click="goDetail(item, index)">
									<view class="content_text">
										<text>{{ item && item.title && item.title.value }}</text>
									</view>
									
								<!-- <button class="width-auto upload-btn" v-show="item && item.isLoadData" @click.stop="uploadSampleData(item, index)">
									{{ item.isLoadData === true ? '上传' : item.isLoadData }}
								</button> -->
								<!-- <button type="primary" plain="true" v-show="item && item.isLoadData" @click.stop="uploadSampleData(item, index)">{{ item.isLoadData === true ? '上传' : item.isLoadData }}</button> -->
									<button type="primary" plain="true" class="width-auto get-location content_button"v-show="item && item.isLoadData" @click.stop="uploadSampleData(item, index)">{{ item.isLoadData === true ? '上传' : item.isLoadData }}</button>
								</view>
								
							</uni-swipe-action-item>
							
						</uni-swipe-action>
						
					</view>
			<!-- 		<uni-swipe-action>
						<uni-swipe-action-item v-for="(item, index) in listData" :key="index" :right-options="item.options"  @click="bindClick" @change="swipeChange($event, index)">
							<view class="content-box"  @click="goDetail(item, index)">
								<text>{{ item && item.title && item.title.value }}</text>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action> -->
				
			</view>
		</view>
		<!-- <button type="primary" class="primary" @tap="goSpeech">语音识别</button> -->
	</view>
</template>
<script>
import net from '../../net.js';
import service from '../../service.js';
import utils from '../../common/utils.js';
import uploaddata from '../../uploaddata.js';

// import uniSwipeAction from '../../components/uni-swipe-action';
// import uniSwipeActionItem from '../../components/uni-swipe-action-item';

export default {
	// components: {uniSwipeAction,uniSwipeActionItem},
	data() {
		return {
			listData: [],
			loadingType: 0,
			val: '',
			barNum:'',
			options:[
			        {
			            text: '下载',
			            style: {
			                backgroundColor: '#007aff',
			            }
			        }, {
			            text: '进入',
			            style: {
			                backgroundColor: '#dd524d',
			            }
			        }
			      ]
		};
	},

	onNavigationBarSearchInputClicked(e) {
		//监听原生标题栏搜索输入框点击事件
		console.log('跳转到搜索页');
		this.seachLive(this.val);
	},

	// 加载数据
	onLoad() {
		this.listData = [];
		this.loadingType = 0;
		this.val = '';
		let user = service.getUsers(); //获取用户信息
		if (Object.keys(user).length == 0) {
			uni.reLaunch({
				url: '/pages/login/login'
			});
		} else {
		//	uni.switchTab({
		//		url: '/pages/main/main'
		//	});
			this.listData = service.getTaskList(); //获取任务列表
			// console.log(this.listData);
			
			//设置角标
			this.setBadge();
			
			if (this.listData.length == 0) {
				this.getList();
			
			}
		}
	},

	// 下拉刷新
	onPullDownRefresh() {
		// uni.showModal({
		// 	title: '提示',
		// 	content: '是否更新数据？',
		// 	showCancel: true,
		// 	success: res => {
		// 		if (res.confirm) {
		// 			this.loadingType = 0;
		// 			this.listData = [];
		// 			this.getList();
		// 		} else {
		// 			uni.stopPullDownRefresh(); //停止当前页面下拉刷新。
		// 		}
		// 	}
		// });
		this.loadingType = 0;
		this.listData = [];
		this.getList();
		
	},
	//当页面重新返回主页时数据更新
	// 		onShow() {
	// 			this.getList();
	//
	// 		},

	methods: {
		// upvido(){
		// 	console.log('上传');
		// 	uni.chooseVideo({	
		// 					maxDuration:60,//拍摄视频最长拍摄时间，单位秒。最长支持 60 秒
		// 					count: 4,
		// 					// camera: this.cameraList[this.cameraIndex].value,//'front'、'back'，默认'back'
		// 					sourceType: ['album', 'camera'],
		// 					success:res =>{
		// 						console.log('video---',res);
		// 					}
		// 				})
		// },
		bindClick(e,index){
		      // console.log('点击了'+(e.position === 'left' ? '左侧' : '右侧') + e.content.text + '按钮')
			  // console.log(e);
			  if(e.content.text == '下载'){
				  console.log('下载',index);
				  let codeID = this.listData[index].code.value;
				  console.log(codeID);
				  net.netGet(
				  		'/module/api/main/getSampleInfoNew/' + encodeURIComponent(codeID),
				  		{},
				  		res => {
				  			console.log(res, 'res');
							let data = {};
				  			if (res != null && Object.keys(res).length != 0) {
				  				 data= res;
				  				// 保存 this.codeId 的数据
				  			
				  				uni.showToast({ icon: 'loading', title: '正在下载数据' });
				  			} else {
				  				uni.showToast({ icon: 'none', title: '暂无数据' });
				  			}
				  			service.setTaskDetail(codeID, data);
							uni.showToast({ icon: 'none', title: '下载成功' });
				  		},
				  		() => {
				  			uni.showToast({ icon: 'none', title: '无法下载数据' });
				  		},
				  	
				  	);
				  
			  }else{
				  console.log('进入',index);
				   let data = this.listData[index];
				   let param = utils.toParam({
						data: data,
						index: index,
						isLogo:'local',
				   });
				   uni.navigateTo({
				   	url: './detail' + param
				   });
			  }
		    },
		swipeChange(e,index){
		      // console.log('当前状态：'+ open +'，下标：' + index)
			  console.log(e,index);
		    },
		//设置角标
		setBadge(){
			this.barNum = this.listData.length.toString();
			console.log(this.barNum);
			uni.setStorageSync('BAR_NUM', this.barNum);
			uni.setTabBarBadge({
				index: 0,
				text: this.barNum,
			})
		},
		
		//点击跳转任务详情
		goDetail(data, index) {
			let param = utils.toParam({
				data: data,
				index: index
			});

			// 设置 更新首页数据的回调
			utils.setMainCallBack((res, isLoadData) => {
				data.isLoadData = isLoadData;
				service.setTaskIndex(data, index);
			});

			uni.navigateTo({
				url: './detail' + param
			});
		},
		getList() {
			if (this.loadingType != 0) {
				return;
			}
			this.loadingType = 1; //加载状态
			net.netGet(
				'/module/api/main/getTaskData',
				{},
				res => {
					console.log(res)
					if (res != null && Array.isArray(res) && res.length != 0) {
						this.listData = res;
						uni.showToast({
							icon: 'none',
							title: '正在更新数据',
							duration: 500
						});
						service.setTaskList(this.listData);
						//更新角标
						this.setBadge();
						
					} else {
						uni.showToast({
							icon: 'none',
							title: '暂无数据'
						});
					}
				},
				() => {
					uni.showToast({
						icon: 'none',
						title: '无法更新数据'
					});
				},
				res => {
					//console.log(JSON.stringify(res));
					this.loadingType = 0;
					uni.stopPullDownRefresh(); //停止当前页面下拉刷新。
				}
			);
		},
		uploadSampleData(data, index) {
			// 设置 更新首页数据的回调
			utils.setMainCallBack((res, isLoadData) => {
				data.isLoadData = isLoadData;
				service.setTaskIndex(data, index);
			});
			uploaddata.setUploadItem(data);
			uploaddata.uploadSampleData();
		},
		goSpeech() {
			uni.navigateTo({
				url: './e_signature'
			});
		},
		seachLive() {
			uni.navigateTo({
				url: `./getTaskData?title=${this.val}`
			});
		}
	}
};
</script>

<style lang="less" scoped>
.page {
	padding-top: 20upx;
	height: 100%;
	width: 100%;
}
.get-location {
	font-size: 30rpx;
	padding: 4rpx 8rpx;
	margin: auto 0;
	line-height: 50rpx;
	flex: 1;
}
page {
	background: #efeff4;
}
uni-swipe-action{
	flex: 1;
}
.content-box {
		flex: 1;
		height: 70rpx;
		line-height: 70rpx;
		padding: 10rpx 15rpx;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
		.content_text{
			width: 85%;
			display: inline-block;
		}
		.content_button{
			width: 80rpx;
			display: inline-flex;
		}
}
.con{
	height: 100%;
}
/* .uni-list {
	height: 100%;
} */
.uni-list-cell:after {
	left: 0;
}

.uni-media-list-body {
	height: auto;
	justify-content: space-around;
}

/* .upload-btn {
	font-size: 32upx;
	padding: 10upx 18upx;
	line-height: 14upx;
	margin: 0;
} */
</style>
